//
//  BingMapTileProvider.m
//  SPMapKit
//
//  Created by zhangbo on 2017/9/7.
//  Copyright © 2017年 zhangbo. All rights reserved.
//

#import "BingMapTileProvider.h"

@implementation BingMapTileProvider

-(NSString*) tileImageUrl:(NSInteger) row  col:(NSInteger) col level:(NSInteger) level mapType:(SPMapType) mapType
{
    NSString *tileId = [self TileXYToQuadKey:row tileY:col levelOfDetail:level];
    NSString *url = @"";
    switch (mapType) {
        case SPMapTypeStandard:
            url = [NSString stringWithFormat:@"https://r1.tiles.ditu.live.com/tiles/r%@.jpeg?g=100&mkt=zh-cn", tileId];
            break;
            
        case SPMapTypeSatellite:
            url = [NSString stringWithFormat:@"https://ecn.t1.tiles.virtualearth.NET/tiles/a%@.jpeg?g=517", tileId];
            break;
            
        case SPMapTypeHybrid:
            url = [NSString stringWithFormat:@"https://ecn.t1.tiles.virtualearth.NET/tiles/a%@.jpeg?g=517", tileId];
            break;
            
        default:
            url = [NSString stringWithFormat:@"https://r1.tiles.ditu.live.com/tiles/r%@.jpeg?g=100&mkt=zh-cn", tileId];
            break;
    }
    
    return url;
}

-(CGPoint) GPSToContentOffset:(CLLocationCoordinate2D) gpspoint level:(NSInteger) level mapType:(SPMapType) mapType
{
    double pixelX = 0;
    double pixelY = 0;
    
    if (mapType == SPMapTypeStandard) {
        double mgLat, mgLon;
        convertGPS84_To_Gcj02(gpspoint.latitude, gpspoint.longitude, &mgLat, &mgLon);
        LatLongToPixelXY(mgLat, mgLon, (int)level, &pixelX, &pixelY);
    }else{
        LatLongToPixelXY(gpspoint.latitude, gpspoint.longitude, (int)level, &pixelX, &pixelY);
    }
    
    return CGPointMake(pixelX, pixelY);
}

-(CLLocationCoordinate2D) ContentOffsetToGPS:(CGPoint) offset level:(NSInteger) level mapType:(SPMapType) mapType
{
    double lat = 0;
    double lon = 0;
    
    PixelXYToLatLong(offset.x, offset.y, (int)level, &lat, &lon);
    
    if (mapType == SPMapTypeStandard) {
        double mgLat = 0;
        double mgLon = 0;
        convertGcj02_To_GPS84(lat, lon, &mgLat, &mgLon);
        
        return CLLocationCoordinate2DMake(mgLat, mgLon);
    }
    
    return CLLocationCoordinate2DMake(lat, lon);
}

-(CGPoint) GPSToTileOffset:(CLLocationCoordinate2D) gpspoint level:(NSInteger) level mapType:(SPMapType) mapType
{
    double pixelX = 0;
    double pixelY = 0;
    LatLongToPixelXY(gpspoint.latitude, gpspoint.longitude, (int)level, &pixelX, &pixelY);
    
    return CGPointMake(pixelX, pixelY);
}

-(CGFloat) GroundResolution:(CGFloat) lat level:(int) level
{
    return GroundResolution(lat, level);
}

-(NSString*) TileXYToQuadKey:(NSInteger) tileX tileY:(NSInteger) tileY levelOfDetail:(NSInteger) levelOfDetail
{
    NSMutableString *quadKey = [[NSMutableString alloc] init];
    for (int i = (int)levelOfDetail; i >0; i--)
    {
        char digit = '0';
        int mask = 1 << (i - 1);
        if ((tileX & mask) != 0)
        {
            digit++;
        }
        if ((tileY & mask) != 0)
        {
            digit++;
            digit++;
        }
        [quadKey appendFormat:@"%c", digit ];
    }
    return quadKey;
}

@end
